/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.mapper;

import com.koron.purpose.bean.ContractQueryBean;
import com.koron.purpose.bean.PurposeContractBean;
import com.koron.purpose.domain.PurposeContract;
import com.koron.purpose.domain.PurposeContractPersons;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PurposeContractMapper {
	
	@Insert("insert into purpose_contract(id,add_user,add_user_name,add_time,update_user,update_user_name,update_time,"
			+ "name,code,year,object_type,isdirectly,object_id,object_name,staff_liable,state,fstatusflag,note,template_id,"
			+ "template_name,template_content,contract_content,bill_type,staff_name) "
			+ "values (#{id},#{addUser},#{addUserName},#{addTime},#{updateUser},#{updateUserName},#{updateTime},"
			+ "#{name},#{code},#{year},#{objectType},#{isdirectly},#{objectId},#{objectName},#{staffLiable},#{state},"
			+ "#{fstatusflag},#{note},#{templateId},#{templateName},#{templateContent},#{contractContent},#{billType},#{staffName})")
	Integer insert(PurposeContract bean);
	
	@Select("select * from purpose_contract where id=#{id}")
	PurposeContractBean queryById(@Param("id") String id);
	
	@Delete("delete from purpose_contract where id=#{id}")
	Integer delete(@Param("id") String id);
	
	@Update("update purpose_contract set update_user=#{updateUser},update_user_name=#{updateUserName}"
			+ ",update_time=#{updateTime},name=#{name},code=#{code},year=#{year},object_type=#{objectType}"
			+ ",isdirectly=#{isdirectly},object_id=#{objectId},object_name=#{objectName},note=#{note},"
			+ "template_id=#{templateId},template_name=#{templateName},template_content=#{templateContent},contract_content=#{contractContent}"
			+ " where id=#{id}")
	Integer update(PurposeContract bean);
	
	@Update("update purpose_contract set update_user=#{updateUser},update_user_name=#{updateUserName}"
			+ ",update_time=#{updateTime},state=#{state},note=#{note}"
			+ " where id=#{id}")
	Integer signed(PurposeContract bean);
	
	@Update("update purpose_contract set state=#{state} where id=#{id}")
	Integer updateState(@Param("id") String id,@Param("state") int state);
	
	@Update("update purpose_contract set fstatusflag=#{fstatusflag},update_time = NOW() where id=#{id}")
	Integer updateFstatusflag(@Param("id") String id,@Param("fstatusflag") int fstatusflag);
	
	@Select("<script>"
			+ "select * from purpose_contract where 1=1 "
			+ "<if test='name!=null'> and name like #{name} </if>"
			+ "<if test='year!=null'> and year = #{year} </if>"
			+ "<if test='objectType!=null'> and object_type = #{objectType} </if>"
			+ "<if test='objectId!=null'> and object_id = #{objectId} </if>"
			+ "<if test='staffLiable!=null'> and staff_liable = #{staffLiable} </if>"
			+ "<if test='addUser!=null'> and add_user = #{addUser} </if>"
			+ "<if test='state!=null'> and state = #{state} </if>"
			+ "<if test='billType!=null'> and bill_type = #{billType} </if>"
			+ "<if test='fstatusflag!=null'> and fstatusflag = #{fstatusflag} </if>"
			+ "order by add_time desc"
			+ "<if test='start!=null and end!=null'> limit #{start},#{end} </if>"
			+ "</script>")
	List<PurposeContractBean> query(ContractQueryBean queryBean);
	
	@Select("<script>"
			+ "select count(*) from purpose_contract where 1=1 "
			+ "<if test='name!=null'> and name like #{name} </if>"
			+ "<if test='year!=null'> and year = #{year} </if>"
			+ "<if test='objectType!=null'> and object_type = #{objectType} </if>"
			+ "<if test='objectId!=null'> and object_id = #{objectId} </if>"
			+ "<if test='staffLiable!=null'> and staff_liable = #{staffLiable} </if>"
			+ "<if test='addUser!=null'> and add_user = #{addUser} </if>"
			+ "<if test='state!=null'> and state = #{state} </if>"
			+ "<if test='billType!=null'> and bill_type = #{billType} </if>"
			+ "<if test='fstatusflag!=null'> and fstatusflag = #{fstatusflag} </if>"
			+ "</script>")
	Integer count(ContractQueryBean queryBean);


	@Select("<script>"
			+ "select * from purpose_contract where (add_user = #{userCode} OR staff_liable = #{userCode})"
			+ "<if test='queryBean.name!=null'> and name like #{queryBean.name} </if>"
			+ "<if test='queryBean.year!=null'> and year = #{queryBean.year} </if>"
			+ "<if test='queryBean.objectType!=null'> and object_type = #{queryBean.objectType} </if>"
			+ "<if test='queryBean.objectId!=null'> and object_id = #{queryBean.objectId} </if>"
			+ "<if test='queryBean.staffLiable!=null'> and staff_liable = #{queryBean.staffLiable} </if>"
			+ "<if test='queryBean.addUser!=null'> and add_user = #{queryBean.addUser} </if>"
			+ "<if test='queryBean.state!=null'> and state = #{queryBean.state} </if>"
			+ "<if test='queryBean.billType!=null'> and bill_type = #{queryBean.billType} </if>"
			+ "<if test='queryBean.fstatusflag!=null'> and fstatusflag = #{queryBean.fstatusflag} </if>"
			+ "order by add_time desc"
			+ "<if test='queryBean.start!=null and queryBean.end!=null'> limit #{queryBean.start},#{queryBean.end} </if>"
			+ "</script>")
	List<PurposeContractBean> signQuery(@Param("userCode") String userCode, @Param("queryBean") ContractQueryBean queryBean);

	@Insert("<script>" +
			"insert into purpose_contract_persons(id,purpose_contract_id,add_user,add_user_name,add_time," +
			"update_user,update_user_name,update_time,person_code,person_name,department_code,department_name) values" +
			"<foreach collection ='persons' item='person' separator =','>" +
			"(#{person.id},#{person.purposeContractId},#{person.addUser},#{person.addUserName},#{person.addTime}," +
			"#{person.updateUser},#{person.updateUserName},#{person.updateTime}," +
			"#{person.personCode},#{person.personName},#{person.departmentCode},#{person.departmentName})" +
			"</foreach>" +
			"</script>")
	Integer batchInsertPCPersons(@Param("persons") List<PurposeContractPersons> persons);

	@Delete("delete from purpose_contract_persons where purpose_contract_id = #{purposeContractId}")
	Integer deletePersonsByPurposeContractId(String purposeContractId);

	@Select("select person_code AS personCode, person_name AS personName,department_code AS departmentCode,department_name AS departmentName " +
			"from purpose_contract_persons where purpose_contract_id = #{purposeContractId}")
	List<PurposeContractPersons> queryPersonsByPurposeContractId(String purposeContractId);

	@Update("update purpose_contract set object_type=#{objectType},fstatusflag=#{fstatusflag},update_time = NOW() where id=#{id} and fstatusflag='0'")
	Integer updateFstatusflagById(@Param("id") String id, @Param("objectType") Integer objectType, @Param("fstatusflag") int fstatusflag);

	@Select("<script>"
			+ "select count(*) from purpose_contract where (add_user = #{userCode} OR staff_liable = #{userCode})"
			+ "<if test='queryBean.name!=null'> and name like #{queryBean.name} </if>"
			+ "<if test='queryBean.year!=null'> and year = #{queryBean.year} </if>"
			+ "<if test='queryBean.objectType!=null'> and object_type = #{queryBean.objectType} </if>"
			+ "<if test='queryBean.objectId!=null'> and object_id = #{queryBean.objectId} </if>"
			+ "<if test='queryBean.staffLiable!=null'> and staff_liable = #{queryBean.staffLiable} </if>"
			+ "<if test='queryBean.addUser!=null'> and add_user = #{queryBean.addUser} </if>"
			+ "<if test='queryBean.state!=null'> and state = #{queryBean.state} </if>"
			+ "<if test='queryBean.billType!=null'> and bill_type = #{queryBean.billType} </if>"
			+ "<if test='queryBean.fstatusflag!=null'> and fstatusflag = #{queryBean.fstatusflag} </if>"
			+ "</script>")
	Integer signCount(@Param("userCode") String userCode, @Param("queryBean") ContractQueryBean queryBean);
}
